﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyQuanCafe.BusinessLogic;
using QuanLyQuanCafe.BusinessObject;

namespace QuanLyQuanCafe
{
    public partial class FrmNhaCungCap : Form
    {
        
        NhaCungCapBL nhacungcapbl=new NhaCungCapBL();
        private int trangthai = 0;

        public FrmNhaCungCap()
        {
            InitializeComponent();
        }

        private void FrmNhaCungCap_Load(object sender, EventArgs e)
        {
            gvDanhSachNCC.DataSource = nhacungcapbl.LayDanhSachNCC();
            AnHienButton(false);
            btXoa.Enabled = false;
        }


        private void AnHienButton(bool flag)
        {
            btThem.Enabled = !flag;
            btSua.Enabled = !flag;
            //btXoa.Enabled = !flag;
            btOk.Enabled = flag;
            btHuy.Enabled = flag;

            tbMaNCC.Enabled = flag;
            tbTenNCC.Enabled = flag;

            gvDanhSachNCC.Enabled = !flag;
        }

        private void CapNhatText()
        {
            tbMaNCC.Text = "";
            tbTenNCC.Text = "";
        }
        private void btThem_Click(object sender, EventArgs e)
        {
            AnHienButton(true);
            CapNhatText();
            trangthai = 1;
        }

        private NhaCungCapBO LayThongTinNhaCC()
        {
            NhaCungCapBO nhacungcapbo = new NhaCungCapBO();
            nhacungcapbo.MaNCC = tbMaNCC.Text;
            nhacungcapbo.TenNCC = tbTenNCC.Text;
            return nhacungcapbo;
        }

        private void btOk_Click(object sender, EventArgs e)
        {
            try
            {
                NhaCungCapBO nhacungcapbo = LayThongTinNhaCC();
                if (trangthai==1)
                {
                    if (nhacungcapbl.ThemNhaCC(nhacungcapbo))
                        FrmNhaCungCap_Load(sender, e);
                }
                if (trangthai==2)
                {
                    if (nhacungcapbl.SuaNhaCC(nhacungcapbo))
                       FrmNhaCungCap_Load(sender,e);               
                }
            }
            catch
            {
                MessageBox.Show("Kiểm Lại Các Thông Tin Đã Nhập.", "Quán Lý Quán Cafe", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btSua_Click(object sender, EventArgs e)
        {
            trangthai = 2;
            AnHienButton(true);
            tbMaNCC.Enabled = false;
        }

        private void btXoa_Click(object sender, EventArgs e)
        {          
            if (MessageBox.Show("Bạn Có Muốn Xóa Nhà Cung Cấp [" + tbTenNCC.Text + "] Không?", "Quản Lý Quán Cafe", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                if (nhacungcapbl.XoaNhaCC(tbMaNCC.Text))
                    FrmNhaCungCap_Load(sender, e);
            }
        }

        private void btHuy_Click(object sender, EventArgs e)
        {
            AnHienButton(false);
        }

        private void gvDanhSachNCC_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            int row = e.RowIndex;
            try
            {
                tbMaNCC.Text = gvDanhSachNCC.Rows[row].Cells["MaNCC"].Value.ToString();
                tbTenNCC.Text = gvDanhSachNCC.Rows[row].Cells["TenNCC"].Value.ToString();
            }
            catch
            {
            }
        }

    }
}
